<?php
die('??');


set_time_limit(0);
//include './conf/dbConfig.php';

//$db = new PDO("mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbName'] . "", $dbConfig['username'], $dbConfig['password'], array(PDO::ATTR_PERSISTENT => true));

$narratorsQ = $db->query('SELECT DISTINCT  `narrator` 
FROM  `bukhari` 
ORDER BY  `narrator` ASC ');

$narrators = $narratorsQ->fetchAll(PDO::FETCH_COLUMN);

$sth = $db->prepare('SELECT count(`narrator`) FROM `bukhari` WHERE `narrator` = ?');
$insertSth = $db->prepare('INSERT INTO bukhari_narrators (narrator_name, number_of_narration) VALUES (?, ?)');
$updateSth = $db->prepare('UPDATE bukhari set narrator = ? WHERE narrator = ?');
foreach ($narrators as $narrator) {
    $sth->execute(array($narrator));
    $nuberOfNarrations = $sth->fetchAll(PDO::FETCH_COLUMN);
    
    $insertSth->execute(array($narrator, $nuberOfNarrations[0]));
    
    $lastId = $db->lastInsertId();
    $updateSth->execute(array($lastId, $narrator));
}
